#include<bits/stdc++.h>
#define ll long long
#define db double
#define Si set<int>::iterator
using namespace std;
int const N=2e5+5,L=1e6+5;
ll read()
{
    char x=getchar();
    ll s=0,f=1;
    while(x<'0'||x>'9')
    {
        if(x=='-')f=-f;
        x=getchar();
    }
    while('0'<=x&&x<='9')s=s*10+x-'0',x=getchar();
    return s*f;
}
ll s[N],a[N],f[N],g[L];
ll inline get(int l,int r)
{
    return s[r]-s[l];
}
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);/**/
    int T,i,j,n;T=read();
    while(T--)
    {
        ll ans=0,res=0;
        n=read();
        for(i=1;i<=n;i++)
        {
            a[i]=read();
        }
        for(i=2;i<=n;i++)
        {
            s[i]=s[i-1]+(a[i]==a[i-1]?a[i]:0);
        }
        memset(f,0,sizeof(f));
        memset(g,0,sizeof(g));
        for(i=1;i<=n;i++)
        {
            if(i>=3)
            {
                g[a[i-2]]=max(g[a[i-2]],-s[i-2+1]+f[i-2+1]+a[i-2]);
                res=max(res,-s[i-2+1]+f[i-2+1]);
            }
            f[i]=max(res,g[a[i]])+s[i-1];
        }
        for(i=1;i<=n;i++)
        {
            ans=max(ans,f[i]+s[n]-s[i]);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
